/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dbutil;

import java.io.*;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class JdbcUtil {
    private static String driver = null;
    private static String url = null;
    private static String user = null;
    private static String pwd = null;

    static {
        try {
            loadConfig();
 //           System.out.println(driver + " url:" + url + " user:" + user + " pwd:" + pwd);
            Class.forName(driver);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ExceptionInInitializerError();
        }
    }

    public static Connection getConnection() throws SQLException {
//        System.out.println(driver + " url:" + url + " user:" + user + " pwd:" + pwd);
        return java.sql.DriverManager.getConnection(url, user, pwd);
    }

    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } finally {
            try {
                if(st != null) {
                    try {
                        st.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } finally {
                if(conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
     }

    private static void loadConfig() throws IOException {
        Properties pro = new Properties();
        pro.load(JdbcUtil.class.getClassLoader().getResourceAsStream("config.properties"));
//        String filename = System.getProperty("user.dir") + File.separator + "config.properties";
//    	FileInputStream in = new FileInputStream(new File(filename));
//    	pro.load(in);
        driver = pro.getProperty("driver");
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        pwd = pro.getProperty("pwd");
    }
    
    public static void main(String args[]) throws Exception{
            JdbcUtil.getConnection();
 //        Connection c = JdbcUtil.getConnection();
//        System.out.println(c);
//        String sql = "select * from back_book where identify=?";
//        PreparedStatement ps = c.prepareStatement(sql);
//        ps.setInt(1, 1);
//        ResultSet rs = ps.executeQuery();
//        while(rs.next()) {
//            Object o1 = rs.getObject(1);
//            Object o2 = rs.getObject(2);
//            System.out.println(o1 + " " + o2);
//
//        }
//        JdbcUtil.free(null, null, c);
    }
    
}
